<%@page import="com.sun.xml.internal.ws.api.server.Module"%>
<%@page import="java.util.Calendar"%>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@page import="controller.ModuleExamController, dto.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.SimpleDateFormat" %>

<%
    String type = null;
    int maTS = 0, id = 0;
    String hoten = null, username = null;
    HttpSession ss = request.getSession();
    if (ss.getAttribute("isLogin") == null) {
        response.sendRedirect("login.jsp");
    } else if (ss.getAttribute("isLogin").equals(false)) {
        response.sendRedirect("login.jsp");
    } else {
        type = ss.getAttribute("type").toString();
        if (type.equals("TS")) {
            maTS = Integer.parseInt(ss.getAttribute("maTS").toString());
            hoten = ss.getAttribute("hoten").toString();
        } else {
            id = Integer.parseInt(ss.getAttribute("id").toString());
            username = ss.getAttribute("username").toString();
        }
    }

    String maDT = request.getParameter("maDT");
    if (maDT == null) {
        return;
    }
    if (type.equals("QT")) {
        return;
    }
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Thi trắc nghiệm online</title>
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        <script type="text/javascript" src="js/general_functions.js"></script>
        <link rel="stylesheet" href="css/thi_lambai.css" type="text/css" />
        <script type="text/javascript" src="js/thi_lambai.js"></script>
    </head>
    <body>

        <!-- BEGIN Header -->
        <div id="header">
            <div id="header-content">
                <h2 id="slogan">Thi trắc nghiệm online</h2>
                <ul id="header-links">
                    <li>
                        <%
                            if (type.equals("TS")) {
                                out.print(hoten);
                            } else {
                                out.print(username);
                            }
                        %>
                    </li>
                    <li><a href="logout.jsp">Đăng xuất</a></li>
                </ul>
                <!-- Menu Tabs -->
                <ul>
                    <li><a href="index.jsp" title="Trang chủ"><span>Trang chủ</span></a></li>
                    <li><a href="thi.jsp" title="Thi trắc nghiệm"><span>Thi trắc nghiệm</span></a></li>
                    <li><a href="thongke.jsp" title="Tổng hợp kết quả thi"><span>Tổng hợp kết quả thi</span></a></li>
                    <li><a href="cauhoi.jsp" title="Soạn câu hỏi"><span>Soạn câu hỏi</span></a></li>
                </ul>
            </div>
        </div>
        <!-- END Header -->


        <!-- BEGIN Content -->
        <div id="content">
            <h1 style="text-align:center">LÀM BÀI THI</h1>

            <div class="baithi_info">
                <%
                    ModuleExamController ctrl = new ModuleExamController();
                    ResultSet rs = ctrl.getLoaiDeThiByMaDT(maDT);
                    out.println("<table>");
                    int thoigianlambai = 0;
                    if (rs.next()) {
                        out.println("<tr><th>Thí sinh</th><td>" + hoten + "</td></tr>");
                        out.println("<tr><th>Mã đề thi </th><td>" + maDT + "</td></tr>");
                        out.println("<tr><th>Loại đề thi </th><td>" + rs.getString("tenloai") + "</td></tr>");
                        out.println("<tr><th>Thời gian làm bài </th><td><b>" + Integer.toString(rs.getInt("thoigian")) + " phút" + "</b></td></tr>");
                        thoigianlambai = rs.getInt("thoigian");
                    } else {
                        return;
                    }
                    rs.close();
                    out.println("</table>");

                    // thêm vào table baithi
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String sysDate = sdf.format(calendar.getTime());
                    try {
                        boolean result = ctrl.insertBaiThi(maDT, maTS, sysDate);
                        if (result == false) {
                            out.println("<h1>Lỗi thêm bài thi.</h1>");
                            return;
                        }
                    } catch (Exception e) {
                        out.println("<h1>Lỗi thêm bài thi : " + e.getMessage() + "</h1>");
                        return;
                    }
                %>
            </div>

            <div class="countdown">Thời gian còn lại : <span id="javascript_countdown_time"></span></div>

            <form action="gui_handling/thi_ketqua.jsp" method="get" name="form_baithi">

                <div class="baithi_noidung">
                    <%
                        rs = ctrl.getChiTietDeThi(maDT);
                        //rs = query.executeQuery("select * from ct_dethi join cauhoi on cauhoi = maCH where dethi = " + maDT + " order by thutu asc");
                        int i = 1;
                        while (rs.next()) {
                            out.println("<div class='cauhoi'>");
                            out.println("<div class='noidung'><b>Câu hỏi " + Integer.toString(i) + " : </b>" + rs.getString("noidung") + "</div>");
                            // lựa chọn 1
                            out.println("<div class='luachon'>");
                            out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_1' value='1' />");
                            out.println("<label for='" + Integer.toString(i) + "_1'>" + rs.getString("luachon1") + "</label>");
                            out.println("</div>");
                            // lựa chọn 2
                            out.println("<div class='luachon'>");
                            out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_2' value='2' />");
                            out.println("<label for='" + Integer.toString(i) + "_2'>" + rs.getString("luachon2") + "</label>");
                            out.println("</div>");
                            // lựa chọn 3
                            out.println("<div class='luachon'>");
                            out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_3' value='3' />");
                            out.println("<label for='" + Integer.toString(i) + "_3'>" + rs.getString("luachon3") + "</label>");
                            out.println("</div>");
                            // lựa chọn 4
                            out.println("<div class='luachon'>");
                            out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_4' value='4' />");
                            out.println("<label for='" + Integer.toString(i) + "_4'>" + rs.getString("luachon4") + "</label>");
                            out.println("</div>");
                            //
                            out.println("</div>");
                            i++;
                        }
                        rs.close();

                        try {
                            rs = ctrl.getBaiThi(maDT, maTS, sysDate);
                            rs.next();
                            out.println("<input type='hidden' name='mabt' value='" + Integer.toString(rs.getInt("maBT")) + "' >");
                            rs.close();
                        } catch (Exception e) {
                            out.println("<h1>Lỗi : " + e.getMessage() + "</h1>");
                            return;
                        }
                    %>
                </div>

                <div class="form_submit">
                    <input type="button" class="mybutton" value="Nộp bài" onclick=" if(confirm('Bạn muốn nộp bài?')) lambai_submit(); ">
                </div>

            </form>

        </div>
        <!-- END Content -->

        <script type='text/javascript' src='js/countdown.js'></script>
        <script type='text/javascript'>
            timeleft_function = function() {
                lambai_submit();
            };
            showtime_function = function(time) {
                window.status = time;
            };
            javascript_countdown.init( <% out.print(thoigianlambai * 60);%> , 'javascript_countdown_time');
        </script>

        <!-- BEGIN footer -->
        <div id="footer">
            <div id="footer-content">
                <div class="footer-left">
                    <p style="text-align: right">
                        &copy; copyright 2006-2011 <strong class="separator">www.styleshout.com</strong>
                    </p>
                </div>
            </div>
        </div>
        <!-- END footer -->

    </body>
</html>
